﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Net;
using System.Threading;
using System.IO;
using System.Text;
using MontagsmalerApp.Utils;

namespace MontagsmalerApp
{
  /// <summary>
  /// Interaktionslogik für "App.xaml"
  /// </summary>
  public partial class App : Application
  {
    Thread httpServerThread = null;
    HttpServer httpServer = null;

    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
      MessageBox.Show(e.Exception.Message);
      e.Handled = true;
    }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
      Views.MainWindow main = new Views.MainWindow();
      ViewModels.Spiel spiel = new ViewModels.Spiel();
      main.DataContext = spiel;
      main.Show();

      httpServer = new MyHttpServer(8091, spiel);

      httpServerThread = new Thread(new ThreadStart(httpServer.listen));
      httpServerThread.Start();
    }

    private void Application_Exit(object sender, ExitEventArgs e)
    {
      if (httpServer != null)
      {
        // Das Stoppen des Thread muss noch angepasst werden!
        httpServer.Stop();
        httpServerThread.Abort();
      }
    }
  }
}
